<?php
/* --------------------------------------------------------------
   PropertiesCombisAdminControl.php 2012-02-27 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2012 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/


class PropertiesCombisAdminControl 
{
	function PropertiesCombisAdminControl() 
	{
	}
	
	function remove_properties_value($p_combis_values_id)
	{
		$c_combis_values_id = (int)$p_combis_values_id;
		
		$coo_combis_values_do = new GMDataObject('products_properties_combis_values');
		$coo_combis_values_do->set_keys(array('products_properties_combis_values_id' => $c_combis_values_id));
		$coo_combis_values_do->delete();
	}

	function get_all_properties_combis_ids($p_products_id)
	{
		$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$t_properties_combis_ids_array = $coo_properties_data_agent->get_all_properties_combis_ids($p_products_id);
		
		return $t_properties_combis_ids_array;
	}
	
	function get_combis_full_struct($p_properties_combis_id, $p_language_id)
	{
		$t_combis_struct = array();
		
		$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$t_combis_struct = $coo_properties_data_agent->get_combis_full_struct($p_properties_combis_id, $p_language_id);
		
		return $t_combis_struct;
	}
	
	function add_properties_value($p_properties_combis_id, $p_properties_values_id)
	{
		$c_properties_combis_id = (int)$p_properties_combis_id;
		$c_properties_values_id = (int)$p_properties_values_id;
		
		$coo_combis_values_do = new GMDataObject('products_properties_combis_values');
		$coo_combis_values_do->set_keys(array('products_properties_combis_values_id' => false));
		
		# set content
		$coo_combis_values_do->set_data_value('products_properties_combis_id', $c_properties_combis_id);
		$coo_combis_values_do->set_data_value('properties_values_id', $c_properties_values_id);
		
		# do insert
		$t_insert_id = $coo_combis_values_do->save_body_data();
		return $t_insert_id;
	}
	
	function delete_properties_combis($p_properties_combis_id_array)
	{
		$c_properties_combis_id_array = $p_properties_combis_id_array;
		
		foreach($c_properties_combis_id_array AS $t_properties_combis_id){
			$c_properties_combis_id = (int)$t_properties_combis_id;
			
			# get related combis_values 
			$coo_data_group = new GMDataObjectGroup('products_properties_combis_values', array('products_properties_combis_id' => $c_properties_combis_id));
			$coo_data_array = $coo_data_group->get_data_objects_array();

			foreach($coo_data_array as $coo_data_item)
			{
				$coo_data_item->delete();
			}

			# get related index entries
			$coo_data_group = new GMDataObjectGroup('products_properties_index', array('products_properties_combis_id' => $c_properties_combis_id));
			$coo_data_array = $coo_data_group->get_data_objects_array();

			foreach($coo_data_array as $coo_data_item)
			{
				$coo_data_item->delete();
			}
			
			# delete products in baskets
			$this->clear_baskets_combis($c_properties_combis_id);
			
			# delete combi
			$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $c_properties_combis_id));
			$t_target_path	= DIR_FS_CATALOG_IMAGES.'product_images/properties_combis_images/';
			$t_filename		= $coo_data_object->get_data_value('combi_image');

			if(empty($t_filename) == false && file_exists($t_target_path.$t_filename))
			{
				#delete image file
				unlink($t_target_path.$t_filename);
			}
			$coo_data_object->delete();
		}
	}
	
	function clear_baskets_combis($p_combis_id)
	{
		$c_combis_id = (int)$p_combis_id;
		
		$t_sql = 'DELETE FROM customers_basket WHERE products_id LIKE "%x'.$c_combis_id.'"';
		xtc_db_query($t_sql);
	}

	function get_combined_ids($p_id_array)
	{
		$t_output_combi_array = array();
		$t_built_combis_array = array();

		$t_first_combi_array = array();
		$t_tail_combi_array = array();

		if(sizeof($p_id_array) == 1)
		{
			$t_op_array = end($p_id_array);
			foreach($t_op_array as $t_item)
			{
				$t_output_combi_array[] = array($t_item);
			}
		}
		elseif(sizeof($p_id_array) > 1)
		{
			$t_tail_combi_array = $p_id_array;
			$t_first_combi_array = array_shift($t_tail_combi_array);

			$t_built_combis_array = $this->get_combined_ids($t_tail_combi_array);
		}

		for($i=0; $i<sizeof($t_first_combi_array); $i++)
		{
			for($h=0; $h<sizeof($t_built_combis_array); $h++)
			{
				$t_combi_array = array_merge(
									array($t_first_combi_array[$i]), 
									$t_built_combis_array[$h]
								);
				//echo implode('-', $t_combi_array) .'<br>';
				$t_output_combi_array[] = $t_combi_array;
			}
			
		}
		return $t_output_combi_array;
	}

	function get_composed_combi_model($p_properties_value_ids_array)
	{
		$t_model_output = '';
		$t_model_array = array();

		# get sort_order and value_model of given value_ids
		foreach($p_properties_value_ids_array as $t_value_key => $t_value_id)
		{
			$t_sql = '
				SELECT
                    p.properties_id AS properties_id,
					p.sort_order AS sort_order,
					pv.value_model AS value_model
				FROM
					properties AS p LEFT JOIN properties_values AS pv USING (properties_id)
				WHERE
					pv.properties_values_id = "'.addslashes($t_value_id).'"
				ORDER BY
					p.sort_order ASC,
					p.properties_id ASC
			';
			$t_result = xtc_db_query($t_sql);
			$t_data = xtc_db_fetch_array($t_result);

			$t_model_output .= $t_data['value_model'];
			if($t_value_key < sizeof($p_properties_value_ids_array)-1) $t_model_output .= '-';
		}
        
		return $t_model_output;
	}

	function autobuild_combis($p_products_id, $p_properties_value_ids_array)
	{	/* parameter sample
		$p_properties_value_ids_array = array(
											'5' => array(48, 56, 64),
											'6' => array(68, 69, 74),
											'8' => array(78, 76, 84)
										);*/
		# find highest given sort_order
		$coo_data_group = MainFactory::create_object('GMDataObjectGroup', array('products_properties_combis', array('products_id' => $p_products_id), array('sort_order') ));
		$t_data_array = $coo_data_group->get_data_objects_array();

		# set start sort_order
		if(sizeof($t_data_array) == 0) {
			$t_sort_order = 0;
		} else {
			$t_sort_order = $t_data_array[0]->get_data_value('sort_order');
		}

		# get all possible combis
		$t_all_combis_array = $this->get_combined_ids($p_properties_value_ids_array);
//		print_r($t_all_combis_array);

		$t_created_combi_id_array = array();

		# save combis and collect combi_ids
		foreach($t_all_combis_array as $t_combi_item_array)
		{
			# build combi_model by used properties_models
			$t_combi_model = $this->get_composed_combi_model($t_combi_item_array);

			$t_data_array = array(
								'products_id' => $p_products_id,
								'sort_order' => $t_sort_order,
								'combi_model' => $t_combi_model,
								'properties_values_ids' => $t_combi_item_array
							);
			$t_created_combi_id_array[] = $this->save_properties_combis($t_data_array, true);
			$t_sort_order++;
		}

		$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$coo_properties_data_agent->rebuild_properties_index($p_products_id);

		return $t_created_combi_id_array;
	}
	
	function save_properties_combis($p_data_array, $p_prevent_duplicate_combis=false)
	{
		if($p_prevent_duplicate_combis) # used in autobuild-mode
		{
			# look for existing combi
			$coo_properties_control = MainFactory::create_object('PropertiesControl');
			$t_products_properties_combis_id = $coo_properties_control->get_combis_id_by_value_ids_array($p_data_array['products_id'], $p_data_array['properties_values_ids']);
			if($t_products_properties_combis_id > 0)
			{
				# existing combi found, return found combi_id and stop saving
				return $t_products_properties_combis_id;
			}
		}

		$t_insert_mode = true;
		
		if(empty($p_data_array['products_properties_combis_id']) == false) 
		{
			# switch to update-mode, if products_properties_combis_id exists
			$t_insert_mode = false;
		}
		
		$coo_combis_do = new GMDataObject('products_properties_combis');
		$products_properties_combis_id = 0;
		
		if($t_insert_mode) {
			# set to false for an insert
			$coo_combis_do->set_keys(array('products_properties_combis_id' => false));
		} 
		else {
			# using a given key will cause an update
			$products_properties_combis_id = (int)$p_data_array['products_properties_combis_id']; 
			$coo_combis_do->set_keys(array('products_properties_combis_id' => $products_properties_combis_id));
		}
		
		$coo_combis_do->set_data_value('products_id', $p_data_array['products_id']);
		$coo_combis_do->set_data_value('sort_order', $p_data_array['sort_order']);
		$coo_combis_do->set_data_value('combi_model', $p_data_array['combi_model']);
		$coo_combis_do->set_data_value('combi_quantity_type', $p_data_array['combi_quantity_type']);
		$coo_combis_do->set_data_value('combi_quantity', $p_data_array['combi_quantity']);
		$coo_combis_do->set_data_value('combi_weight', $p_data_array['combi_weight']);
		$coo_combis_do->set_data_value('combi_price_type', $p_data_array['combi_price_type']);
		$coo_combis_do->set_data_value('combi_price', $p_data_array['combi_price']);
		$coo_combis_do->set_data_value('vpe_value', $p_data_array['vpe_value']);
		$coo_combis_do->set_data_value('products_vpe_id', $p_data_array['products_vpe_id']);
		$coo_combis_do->set_data_value('combi_shipping_status_id', $p_data_array['shipping_status_id']);
		
		if(empty($p_data_array['combi_image']) == false)
		{
			$coo_combis_do->set_data_value('combi_image', $p_data_array['combi_image']);
		}

		if(empty($p_data_array['delete_image']) == false)
		{
			$t_target_path	= DIR_FS_CATALOG_IMAGES.'product_images/properties_combis_images/';
			$t_filename		= $p_data_array['delete_image'];

			if(file_exists($t_target_path.$t_filename))
			{
				#delete file
				unlink($t_target_path.$t_filename);
			}
			#clear filename
			$coo_combis_do->set_data_value('combi_image', '');
		}
		
		$t_insert_id = $coo_combis_do->save_body_data();
		if($t_insert_id > 0) $products_properties_combis_id = $t_insert_id;
		
		# remove all maybe existing properties_values in combination before adding new list
		$coo_combis_values_do = new GMDataObject('products_properties_combis_values');
		$coo_combis_values_do->set_keys(array('products_properties_combis_id' => $products_properties_combis_id));
		$coo_combis_values_do->delete();
		
		# add selected properties_values to properties combination
		foreach($p_data_array['properties_values_ids'] as $t_values_id)
		{
			$c_values_id = (int)$t_values_id;
			if($c_values_id > 0) $this->add_properties_value($products_properties_combis_id, $c_values_id);
		}
		return $products_properties_combis_id;
	}

	function save_admin_select($p_products_id, $p_properties_value_ids_array)
	{
		/*
		$p_properties_value_ids_array = array(
											'5' => array(48, 56, 64),
											'6' => array(68, 69, 74),
											'8' => array(78, 76, 84)
										);
		*/

		# remove all maybe existing selects before adding new list
		$coo_select = new GMDataObject('products_properties_admin_select');
		$coo_select->set_keys(array('products_id' => $p_products_id));
		$coo_select->delete();

		foreach($p_properties_value_ids_array as $t_properties_id => $t_value_ids_array)
		{
			foreach($t_value_ids_array as $t_value_id)
			{
				$coo_select = new GMDataObject('products_properties_admin_select');

				$coo_select->set_data_value('products_id', $p_products_id);
				$coo_select->set_data_value('properties_id', $t_properties_id);
				$coo_select->set_data_value('properties_values_id', $t_value_id);

				$coo_select->save_body_data();
				unset($coo_select);
			}
		}
	}

	function get_admin_select($p_products_id)
	{
		$coo_output_array = array();

		$coo_data_group = new GMDataObjectGroup('products_properties_admin_select', array('products_id' => $p_products_id));
		$coo_data_array = $coo_data_group->get_data_objects_array();
		
		for($i=0; $i<sizeof($coo_data_array); $i++)
		{
			$t_properties_id = $coo_data_array[$i]->get_data_value('properties_id');
			$t_properties_values_id = $coo_data_array[$i]->get_data_value('properties_values_id');

			if(array_key_exists($t_properties_id, $coo_output_array) == false) $coo_output_array[$t_properties_id] = array();
			
			if(is_array($coo_output_array[$t_properties_id]) == false) $coo_output_array[$t_properties_id] = array();

			$coo_output_array[$t_properties_id] = array_merge(
														$coo_output_array[$t_properties_id],
														array($t_properties_values_id)
													);
		}
		return $coo_output_array;
	}

	function reset_combis_sort_order($p_products_id)
	{
            $coo_properties_combis = new GMDataObjectGroup('products_properties_combis', array('products_id' => $p_products_id));
		
            $t_properties_combis_array = $coo_properties_combis->get_data_objects_array();
            
            if(sizeof($t_properties_combis_array) > 0){
                foreach($t_properties_combis_array AS $t_properties_combi){
                    $t_combi_id = $t_properties_combi->get_data_value('products_properties_combis_id');
                    $t_combi_values_array = array();
                    
                    $t_sql = "SELECT 
                                    pv.sort_order
                              FROM 
                                    products_properties_index AS ppi
                              LEFT JOIN
                                    properties AS p ON ppi.properties_id = p.properties_id
                              LEFT JOIN
                                    properties_values AS pv ON ppi.properties_values_id = pv.properties_values_id
                              WHERE
                                    ppi.products_properties_combis_id = $t_combi_id 
                                    AND ppi.language_id = 2
                              ORDER BY
                                    p.sort_order ASC,
                                    p.properties_id ASC,
                                    pv.sort_order ASC,
                                    pv.properties_values_id ASC";
                    
                    $t_result = xtc_db_query($t_sql);
                    while($t_row = xtc_db_fetch_array($t_result)){
                        $t_combi_values_array[] = $t_row['sort_order'];                     
                    }
                    
                    $t_combi_values_string = implode(".", $t_combi_values_array);
                    
                    $start_array[] = array("combis_id" => $t_combi_id, "sort_string" => $t_combi_values_string);
                }
            }     
            
            usort($start_array, array($this, "compare_combis_sort_order"));
            
            foreach($start_array AS $key => $combi){               
                $coo_properties_do = new GMDataObject('products_properties_combis');
		
		$coo_properties_do->set_keys(array('products_properties_combis_id' => $combi['combis_id']));
                
		$coo_properties_do->set_data_value('sort_order', $key);
                
		$coo_properties_do->save_body_data();
            }
            
            return true;
	}
        
        function compare_combis_sort_order($a, $b){
                return strnatcmp($a['sort_string'], $b['sort_string']);
        }

	function get_admin_select_detailed($p_products_id)
	{
		$coo_output_array = array();

		$c_products_id = (int)$p_products_id;
		$c_language_id = (int)$_SESSION['languages_id'];

		$t_sql = '
			SELECT
				pd.properties_id,
				pd.properties_name,
				pd.properties_admin_name,
				pvd.properties_values_id,
				pvd.values_name
			FROM
				products_properties_admin_select AS ppas
					LEFT JOIN properties AS p USING (properties_id)
					LEFT JOIN properties_description AS pd USING (properties_id)
					LEFT JOIN properties_values AS pv USING (properties_values_id)
					LEFT JOIN properties_values_description AS pvd ON (pv.properties_values_id = pvd.properties_values_id)
			WHERE
				ppas.products_id = "'.$c_products_id.'" AND
				pd.language_id = "'.$c_language_id.'" AND
				pvd.language_id = "'.$c_language_id.'"
			GROUP BY
				pvd.properties_values_id
			ORDER BY
				p.sort_order,
				p.properties_id,
				pv.sort_order,
				pv.properties_values_id
		';
		$t_result = xtc_db_query($t_sql);

		while(($t_row = xtc_db_fetch_array($t_result) ))
		{
			$t_properties_id = $t_row['properties_id'];
			$t_properties_name = $t_row['properties_name'];
			$t_properties_admin_name = $t_row['properties_admin_name'];
			$t_properties_values_id = $t_row['properties_values_id'];
			$t_properties_values_name = $t_row['values_name'];

			if(is_array($coo_output_array[$t_properties_id]) == false) $coo_output_array[$t_properties_id] = array(
                                                                                                                                'properties_name' => $t_properties_name,
                                                                                                                                'properties_admin_name' => $t_properties_admin_name,
                                                                                                                                'properties_values_array' => array()
                                                                                                                        );
			$coo_output_array[$t_properties_id]['properties_values_array'] = array_merge(
                                                                                                    $coo_output_array[$t_properties_id]['properties_values_array'],
                                                                                                    array('pv_id-'.$t_properties_values_id => $t_properties_values_name)
                                                                                            );
		}
		return $coo_output_array;
	}

	function get_vpe_selection_array($p_language_id, $p_selected_vpe_id = 0)
	{
		$t_output_array = array();

		$c_language_id = (int)$p_language_id;
		$c_selected_vpe_id = (int)$p_selected_vpe_id;

		$coo_data_group = new GMDataObjectGroup('products_vpe', array('language_id' => $c_language_id));
		$coo_data_array = $coo_data_group->get_data_objects_array();

		for($i=0; $i<sizeof($coo_data_array); $i++)
		{
			$t_selected = false;
			if($coo_data_array[$i]->get_data_value('products_vpe_id') == $c_selected_vpe_id) $t_selected = true;

			$t_output_array[] = array(
									'products_vpe_id' => $coo_data_array[$i]->get_data_value('products_vpe_id'),
									'language_id' => $coo_data_array[$i]->get_data_value('language_id'),
									'products_vpe_name' => $coo_data_array[$i]->get_data_value('products_vpe_name'),
									'selected' => $t_selected
								);
		}
		return $t_output_array;
	}
	
	function get_shipping_status_array($p_language_id)
	{
		$t_output_array = array();

		$c_language_id = (int)$p_language_id;

		$coo_data_group = new GMDataObjectGroup('shipping_status', array('language_id' => $c_language_id));
		$coo_data_array = $coo_data_group->get_data_objects_array();

		for($i=0; $i<sizeof($coo_data_array); $i++)
		{
			$t_output_array[] = array(
									'shipping_status_id' => $coo_data_array[$i]->get_data_value('shipping_status_id'),
									'shipping_status_name' => $coo_data_array[$i]->get_data_value('shipping_status_name')
								);
		}
		return $t_output_array;
	}
	
	function save_properties_dropdown_mode($p_products_id, $p_dropdown_mode_value){
		
		$t_products_id = (int)xtc_db_input($p_products_id);
		$t_dropdown_mode_value = (string)xtc_db_input($p_dropdown_mode_value);
		
		$t_sql = '
			UPDATE 
				products
			SET
				properties_dropdown_mode = "'.$t_dropdown_mode_value.'"
			WHERE
				products_id = "'.$t_products_id.'"
		';
		xtc_db_query($t_sql);
		
		return $t_dropdown_mode_value;
	}
	
	function get_properties_dropdown_mode($p_products_id){
		
		$t_products_id = (int)$p_products_id;
		
		$t_sql = '
			SELECT
				properties_dropdown_mode
			FROM
				products
			WHERE
				products_id = "'.$t_products_id.'"
		';
		$t_result = xtc_db_query($t_sql);

		$t_row = xtc_db_fetch_array($t_result);
		
		$t_dropdown_mode_value = $t_row['properties_dropdown_mode'];
		
		return $t_dropdown_mode_value;
	}
	
	function save_properties_price_show($p_products_id, $p_price_show_value){
		
		$t_products_id = (int)xtc_db_input($p_products_id);
		$t_price_show_value = (string)xtc_db_input($p_price_show_value);
		
		$t_sql = '
			UPDATE 
				products
			SET
				properties_show_price = "'.$t_price_show_value.'"
			WHERE
				products_id = "'.$t_products_id.'"
		';
		xtc_db_query($t_sql);
		
		return $t_price_show_value;
	}
	
	function get_properties_price_show($p_products_id){
		
		$t_products_id = (int)$p_products_id;
		
		$t_sql = '
			SELECT
				properties_show_price
			FROM
				products
			WHERE
				products_id = "'.$t_products_id.'"
		';
		$t_result = xtc_db_query($t_sql);

		$t_row = xtc_db_fetch_array($t_result);
		
		$t_price_show_value = $t_row['properties_show_price'];
		
		return $t_price_show_value;
	}
	
	function save_use_properties_combis_weight($p_products_id, $p_use_properties_combis_weight){
		
		$t_products_id = (int)xtc_db_input($p_products_id);
		$t_use_properties_combis_weight = (string)xtc_db_input($p_use_properties_combis_weight);
		
		$t_sql = '
			UPDATE 
				products
			SET
				use_properties_combis_weight = "'.$t_use_properties_combis_weight.'"
			WHERE
				products_id = "'.$t_products_id.'"
		';
		xtc_db_query($t_sql);
		
		return $t_use_properties_combis_weight;
	}
	
	function get_use_properties_combis_weight($p_products_id){
		
		$t_products_id = (int)$p_products_id;
		
		$t_sql = '
			SELECT
				use_properties_combis_weight
			FROM
				products
			WHERE
				products_id = "'.$t_products_id.'"
		';
		$t_result = xtc_db_query($t_sql);

		$t_row = xtc_db_fetch_array($t_result);
		
		$t_use_properties_combis_weight = $t_row['use_properties_combis_weight'];
		
		return $t_use_properties_combis_weight;
	}
	
	function save_use_properties_combis_quantity($p_products_id, $p_use_properties_combis_quantity){
		
		$t_products_id = (int)xtc_db_input($p_products_id);
		$t_use_properties_combis_quantity = (string)xtc_db_input($p_use_properties_combis_quantity);
		
		$t_sql = '
			UPDATE 
				products
			SET
				use_properties_combis_quantity = "'.$t_use_properties_combis_quantity.'"
			WHERE
				products_id = "'.$t_products_id.'"
		';
		xtc_db_query($t_sql);
		
		return $t_use_properties_combis_quantity;
	}
	
	function get_use_properties_combis_quantity($p_products_id){
		
		$t_products_id = (int)$p_products_id;
		
		$t_sql = '
			SELECT
				use_properties_combis_quantity
			FROM
				products
			WHERE
				products_id = "'.$t_products_id.'"
		';
		$t_result = xtc_db_query($t_sql);

		$t_row = xtc_db_fetch_array($t_result);
		
		$t_use_properties_combis_quantity = $t_row['use_properties_combis_quantity'];
		
		return $t_use_properties_combis_quantity;
	}
	
	function save_use_properties_combis_shipping_time($p_products_id, $p_use_properties_combis_shipping_time){
		
		$t_products_id = (int)xtc_db_input($p_products_id);
		$t_use_properties_combis_shipping_time = (string)xtc_db_input($p_use_properties_combis_shipping_time);
		
		$t_sql = '
			UPDATE 
				products
			SET
				use_properties_combis_shipping_time = "'.$t_use_properties_combis_shipping_time.'"
			WHERE
				products_id = "'.$t_products_id.'"
		';
		xtc_db_query($t_sql);
		
		return $t_use_properties_combis_shipping_time;
	}
	
	function get_use_properties_combis_shipping_time($p_products_id){
		
		$t_products_id = (int)$p_products_id;
		
		$t_sql = '
			SELECT
				use_properties_combis_shipping_time
			FROM
				products
			WHERE
				products_id = "'.$t_products_id.'"
		';
		$t_result = xtc_db_query($t_sql);

		$t_row = xtc_db_fetch_array($t_result);
		
		$t_use_properties_combis_shipping_time = $t_row['use_properties_combis_shipping_time'];
		
		return $t_use_properties_combis_shipping_time;
	}
	
}

?>